#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
|===== Conference Bot Plugin ============
|= Moderator Commands
|===== By: ==============================
|= Patrick Kennedy
|===== Current Version: =================
|= 1.0
|===== Description: =========================================
|= Admins and Mods can use these commands.
|===== Additional Comments: =================================
|= 
|============================================================"""

from utils import *

def init(bot):
	bot.addModCommand("invite", cmd_invite)
	bot.addModCommand("kick", cmd_kick)
	bot.addModCommand("ban", cmd_ban)
	bot.addModCommand("mute", cmd_mute)
	bot.addHook("message", hook_mute)
	bot.addModCommand("blind", cmd_blind)
	bot.addHook("recieve", hook_blind)
	bot.addModCommand("cripple", cmd_cripple)
	bot.addModCommand("status", cmd_status)
	bot.addModCommand("topic", cmd_status)
	
def unload(bot):
	bot.removeCommand(["invite", "kick", "ban", "mute", "blind", "cripple", "status", "topic"])
	bot.removeHook(['hook_mute', 'hook_blind'])

def cmd_invite(bot, user, args):
	"""Invite a user to the room.
	Usage: )invite <username>"""
	jid = getjid(args.lower())
	if jid:
		bot.addUser(jid)
		adduser(jid)
		bot.msg(user, "%s has been invited." % jid)
	
	else:
		bot.msg(user,cmd_invite.__doc__)
	
def cmd_kick(bot, user, target):
	"""Removes a user from the room.
	Usage: )kick <username> [reason]"""
	
	target, reason = gettarget(target)
	
	if not target:
		bot.error(user, cmd_kick.__doc__)
		return
	if not isuser(bot, target):
		bot.error(user, "The user _%s_ does not exist in the bot." % target)
		return
		
	bot.rejectUser(target)
	bot.removeUser(target)
	del userlist[target]
	if reason != "s":
		if reason:
			bot.systoall("<%s> has been kicked by %s. (Reason: %s)" % (getdisplayname(target), getdisplayname(user), reason))
		else:
			bot.systoall("<%s> has been kicked by %s" % (getdisplayname(target), getdisplayname(user)))
		

def cmd_ban(bot, user, target):
	"""Ban and kick a user from the room.
	Usage: )ban <username> [reason]"""
	
	target, reason = gettarget(target)
	
	if not target:
		bot.error(user, cmd_kick.__doc__)
		return
	if not isuser(bot, target):
		bot.error(user, "The user _%s_ does not exist in the bot." % target)
		return
		
	addban(target)
	bot.removeUser(target)
	bot.rejectUser(target)
	if reason != "s":
		if reason:
			bot.systoall("<%s> has been banned by %s. (Reason: %s)" % (getdisplayname(target), getdisplayname(user), reason))
		else:
			bot.systoall("<%s> has been banned by %s" % (getdisplayname(target), getdisplayname(user)))
				
def cmd_stfu(bot, user, args):
	"""Mute the entire room."""
	for target in bot.getRoster():
		target = getjid(target)
		if isuser(bot, target) and isonline(bot, target):
			if args == "s":
				cmd_mute(bot, user, target + " s")
			else:
				cmd_mute(bot, user, target)
		
def cmd_mute(bot, user, target):
	"""Mute/Unmute a user.
	Usage: )mute <username> [reason]
	Note: To unmute a user simply use the command again."""
	
	target, reason = gettarget(target)
	
	if not target:
		bot.error(user, cmd_kick.__doc__)
		return
	if not isuser(bot, target):
		bot.error(user, "The user _%s_ does not exist in the bot." % target)
		return
		
	if delmute(target):
		if reason != 's':
			bot.sys(target, 'You have been unmuted by <%s>' % getdisplayname(user))
			bot.systoall('<%s> has been unmuted by %s.' % (getdisplayname(target), getdisplayname(user)), butnot = [target])
		else:
			bot.sys(user, '<%s> has been unmuted, silently.' % getdisplayname(target))
	elif not isadmin(target) and not user == target:
		addmute(target)
		if reason != 's':
			if reason:
				reason = " (Reason: %s)" % reason
			bot.sys(target, '<%s> has muted you.%s' % (getdisplayname(user), reason))
			bot.systoall('<%s> has been muted by %s.%s' % (getdisplayname(target), getdisplayname(user), reason), butnot = [target])
		else:
			bot.sys(user, '<%s> has been muted, silently.' % getdisplayname(target))
			
def hook_mute(bot, args):
	user = getjid(args[0])
	if ismuted(user):
		bot.sys(user, "You are muted and therefore can't send any messages.\nPlease type ')help' for a list of usable commands.")
		return True
	return False
	
def cmd_blind(bot, user, target):
	"""Prevent a user from seeing messages.
Usage: )blind <user>

Note: This will not prevent the user from speaking."""
	
	target, reason = gettarget(target)
	
	if not target:
		bot.error(user, cmd_kick.__doc__)
		return
	if not isuser(bot, target):
		bot.error(user, "The user _%s_ does not exist in the bot." % target)
		return
		
	if delblind(target):
		if reason != 's':
			bot.sys(target, 'You have been unblinded by <%s>' % getdisplayname(user))
			bot.systoall('<%s> has been unblinded.' % getdisplayname(target), butnot = [target])
		else:
			bot.sys(user, '<%s> has been unblinded, silently.' % getdisplayname(target))
	elif not isadmin(target) and not user == target:
		addblind(target)
		if reason != 's':
			if reason:
				reason = " (Reason: %s)" % reason
			bot.sys(target, '<%s> has blinded you.%s' % (getdisplayname(user), reason))
			bot.systoall('<%s> has been blinded.%s' % (getdisplayname(target), reason), butnot = [target])
		else:
			bot.sys(user, '<%s> has been blinded, silently.' % getdisplayname(target))
	
def hook_blind(bot, args):
	user = getjid(args[0])
	if isblind(user):
		return True
	return False
	
def cmd_cripple(bot, user, target):
	"""Mute and Blind a user.
Usage: /cripple <user> [msg]"""
	cmd_mute(bot, user, target)
	cmd_blind(bot, user, target)
		
def cmd_status(bot, user, msg):
	"""Change the bot's status.
Usage: )status [<command>:]<message>

Example:
* ')status busy:Down for maintanence'

Commands:
* reload - Use stored status.
* offline
* away
* busy
* idle

Note:
* Any commands with nothing after the ':'
will have blank messages."""

	if not msg:
		bot.error(user, cmd_kick.__doc__)
		return

	status = ''
	
	if msg.lower() == "reload" or msg[:msg.find(":")].lower() == "reload":
		bot.setOnline(module.system.status)
		
	status = msg[msg.find(":") + 1:]
	if status:
		clean_status = status
		status = " (Status: %s)" % status
		
	if msg[:msg.find(":")].lower() in ["busy","dnd"] or msg in ["busy","dnd"]:
		bot.setDND(clean_status)
		bot.msg(user, "The bot is now busy.%s" % status)
			
	elif msg[:msg.find(":")].lower() == "away" or msg == "away":
		bot.setAway(clean_status)
		bot.msg(user, "The bot is now away.%s" % status)
				
	elif msg[:msg.find(":")].lower() in ["idle","xa"] or msg in ["idle","xa"]:
		bot.setXA(clean_status)
		bot.msg(user, "The bot is now idle.%s" % status)
			
	elif msg.lower() == "offline" or msg[:msg.find(":")].lower() == "offline":
		bot.Error(user, "This feature not yet implemented.")
		#bot.setUnavailable()
		#bot.msg(user, "The bot is now invisable.")
		
	else:
		module.system.status = msg
		bot.setOnline(msg)
		bot.msg(user, "The bot's status is now: %s" % msg)
		module.save()
